package com.db.protocol.message;

import com.db.protocol.exception.NotParsingValueProtocolException;
import com.db.protocol.exception.ParseProtocolException;
import com.db.protocol.exception.TagMissingProtocolException;
import com.db.protocol.tags.MsgType;
import com.db.protocol.tags.OrderSide;
import com.db.protocol.tags.Tag;

import java.util.Map;

/**
 * @author nulls.narik
 */
public class ReportMessagePerStockAndSide extends ReportMessage {

    public ReportMessagePerStockAndSide(ReportMessage message) {
        put(Tag.CLIENT_ID, message.getClientID());
        put(Tag.TICKER_SYMBOL, message.getStockName());
        put(Tag.ORDER_SIDE, message.getOrderSide());
        put(Tag.QUANTITY, message.getQuantity());
        put(Tag.PRICE, message.getPrice());
    }

    @Override
    public String toString() {
        StringBuilder message = newMsg(MsgType.REPORT);

        message = addPair(message, Tag.CLIENT_ID, get(Tag.CLIENT_ID).toString());
        message = addPair(message, Tag.TICKER_SYMBOL, get(Tag.TICKER_SYMBOL).toString());
        message = addPair(message, Tag.ORDER_SIDE, String.valueOf(((OrderSide) get(Tag.ORDER_SIDE)).value()));
        message = addPair(message, Tag.QUANTITY, get(Tag.QUANTITY).toString());
        message = addPair(message, Tag.PRICE, get(Tag.PRICE).toString());

        return message.toString();
    }

    public String getClientID() {
        return (String) get(Tag.CLIENT_ID);
    }

    public String getStockName() {
        return (String) get(Tag.TICKER_SYMBOL);
    }

    public OrderSide getOrderSide() {
        return (OrderSide) get(Tag.ORDER_SIDE);
    }

    public Integer getQuantity() {
        return (Integer) get(Tag.QUANTITY);
    }

    public Float getPrice() {
        return (Float) get(Tag.PRICE);
    }

    public void add(ReportMessage message) {
        Float price = getPrice();
        Integer quantity = getQuantity();
        Float addPrice = message.getPrice();
        Integer addQuantity = message.getQuantity();
        put(Tag.PRICE, (price * quantity + addPrice * addQuantity) / (quantity + addQuantity));
        put(Tag.QUANTITY, quantity + addQuantity);
    }

    public String getExchangeID() {
        throw new UnsupportedOperationException();
    }
}
